<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\api\controller\v1;
use app\api\model\v1\Feedback as apiFeedbackModel;
use app\api\model\v1\Send as apiSendModel;
use think\facade\Config;
use think\facade\Lang;
use think\facade\Db;

class Feedback extends Base{
    /**
     * 新增编辑留言
    */
    public function consult(){
        // 加载前端配置文件
        Config::load(app()->getBasePath().$this->lang.DS.'config'.DS.'template.php');
        // 加载后台配置文件
        Config::load(app()->getBasePath().'admin'.DS.'config'.DS.'app.php');
        
        if(request()->isPost()){
            $realname = input('realname'); $mobile = input('mobile');
            $telephone = input('telephone'); $email = input('email'); $content = input('content');
            $paramArray = array('realname'=>$realname,'mobile'=>$mobile,'email'=>$email,'content'=>$content);

            // 姓名验证
            if(config('feedback.realname') == 1 and (mb_strlen($realname) > 30 or mb_strlen($realname) < 2)){
                return json($this->msg($paramArray,lang('v_require_realname'),1001));
            }
            // 手机验证
            if(config('feedback.mobile') == 1){
                if(input('lang') == 'cn' and !preg_match("/^1[34578]\d{9}$/", $mobile)){
                    return json($this->msg($paramArray,lang('v_mobile_error'),1002));
                }elseif(mb_strlen($mobile) > 50 or mb_strlen($mobile) < 5){
                    return json($this->msg($paramArray,lang('v_mobile_error'),1002));
                }
            }
            // 电话验证
            if(config('feedback.telephone') == 1 and (mb_strlen($telephone) > 50 or mb_strlen($telephone) < 2)){
                return json($this->msg($paramArray,lang('v_telephone_error'),1003));
            }
            // 邮箱验证
            if(config('feedback.email') == 1 and !filter_var($email, FILTER_VALIDATE_EMAIL)){
                return json($this->msg($paramArray,lang('v_email_error'),1004));
            }
            //内容必须为2-2000个字符
            if(config('feedback.content') == 1 and (mb_strlen($content) > 2000 or mb_strlen($content) < 2)){
                return json($this->msg($paramArray,lang('v_content_error'),1005));
            }

            // 是否启用验证码
            if(config('captcha.feedback') == 1 and input('client') <> 'app' and !captcha_check(input('verify'))){
                return json($this->msg($paramArray,lang('c_verify_error'),1006));
            }

            //属性必填项判断
            $attWhere[] = ['tabledir','=','feedback']; $attWhere[] = ['parentid','=',0]; $attWhere[] = ['lang','=',$this->lang];
            $attributeData = Db::name('attribute')->where($attWhere)->order('sequence desc')->select();
            foreach ($attributeData as $key => $attRs) {
                $postAttValue = input("attvalue".$key."/a");
                $errorTips = lang('m_please_select').str_replace('*','',$attRs['title']);
                // 验证失败
                if(stripos($attRs['title'],'*') > 0 and empty($postAttValue)){
                    return json($this->msg($paramArray,$errorTips,1007));
                }
            }
            
            try{
                // 新增编辑留言
                $feedback_id = apiFeedbackModel::edit(input('id'));//新增
                
                // 开启了邮件发送
                if(config('email.sendmail.feedback') == 1){
                    $mail_template = Db::name('send_template')->where('id',config('email.template.feedback'))->find();
                    if(!empty($mail_template) and !empty('email.admin_email')){
                        $temp_title = $mail_template['temp_title'].'@@';
                        $email_content = str_replace('{mobile}', $mobile, $mail_template['content']);
                        // 发送邮件：1成功，0失败，-1邮件配置错误
                        apiSendModel::email(config('email.admin_email'),$temp_title,$mail_template['reply_address'],$mail_template['send_name'],$email_content,'feedback',$feedback_id);
                    }
                }

                // 开启了短信发送
                if(config('sms.sendsms.feedback') == 1){
                    $sms_template = Db::name('send_template')->where('id',config('sms.template.feedback'))->find();
                    if(!empty($sms_template) and !empty('email.admin_email')){
                        $sms_content = strip_tags($sms_template['content']);//模板内容，过滤html标签
                        $var_data = array($mobile,msubstr(strip_tags($content),0,30,''),$feedback_id);
                        // 发送短信：1成功，0失败，-1非线上环境
                        apiSendModel::sms(config('sms.admin_mobile'),$sms_content,$sms_template['tempid'],$var_data,'feedback');
                    }
                }
                
                return json($this->msg($paramArray,lang('c_feedback_success'),200));
            }catch(\Exception $e){
                return json($this->msg($e->getMessage(),'服务端错误',500));
            }
        }else{
            return json($this->msg('',lang('c_submit_error'),5001));
        }
    }
}